package com.zzyl.serve.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.service.INursingLevelService;
import com.zzyl.serve.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 护理等级Service业务层处理
 *
 * @author zzyl
 * @date 2025-10-13
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    public static final String NURSINGLEVEL_ALL = "nursingLevel:all";

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Integer id) {
        return this.getById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel) {
        boolean result = this.save(nursingLevel);
        clearCache();
        return result ? 1 : 0;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
        boolean result = this.updateById(nursingLevel);
        clearCache();
        return result ? 1 : 0;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingLevelByIds(Integer[] ids) {
        List<NursingLevel> nursingLevels = this.listByIds(Arrays.asList(ids));
        if (!CollectionUtils.isEmpty(nursingLevels)) {
            nursingLevels.forEach(nursingLevel -> {
                if (nursingLevel.getStatus() == 1) {
                    throw new RuntimeException("启用状态下不能删除");
                }
            });
        }
        boolean result = this.removeByIds(Arrays.asList(ids));
        clearCache();
        return  result ? 1 : 0;
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Integer id) {
        boolean result = this.removeById(id);
        clearCache();
        return result ? 1 : 0;
    }

    /**
     * 获取所有启用的护理等级
     *
     * @return 护理等级列表
     */
    @Override
    public List<NursingLevel> getAll() {

        //获取缓存数据
        List<NursingLevel> levelList = (List<NursingLevel>) redisTemplate.opsForValue().get(NURSINGLEVEL_ALL);
        if(!CollectionUtils.isEmpty(levelList)){
            return levelList;
        }
        //查询数据库
        levelList =this.list(Wrappers.<NursingLevel>lambdaQuery().eq(NursingLevel::getStatus,1));
        //缓存数据到redis中
        redisTemplate.opsForValue().set(NURSINGLEVEL_ALL,levelList);
        return levelList;
    }

    //清空缓存
    private void clearCache(){
        redisTemplate.delete(NURSINGLEVEL_ALL);
    }
}
